home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ptv3n5.zip / NEWTEST.CXX < prev    next >
C/C++ Source or Header  |  1992-09-29  |  2KB  |  110 lines

  1. /////////////////////////////////////////////////
  2. //  DYNAMIC MEMORY ALLOCATION LIBRARY
  3. //      newtest.cxx
  4. //
  5. //      Test dynamic memory debugging functions.
  6. //
  7. //      Copyright 1992 Scott Robert Ladd
  8. //      All Rights Reserved
  9. /////////////////////////////////////////////////
  10.  
  11. #include "newdebug.h"
  12. #include "iostream.h"
  13.  
  14. // prototypes
  15. void MyNewHandler();
  16. void MemStatus();
  17. void MemBug(NewDbg::Errors err);
  18.  
  19. int main()
  20.     {
  21.     // install error handlers
  22.     NewDbg::InstallHandler(MemBug); // my system
  23.     set_new_handler(MyNewHandler);  // std. C++
  24.  
  25.     MemStatus();
  26.  
  27.     // allocate a pointer
  28.     long * iptr = new long;
  29.     cout << "long * iptr = new long;" << endl;
  30.     MemStatus();
  31.  
  32.     // set the value at the pointer to zero
  33.     *iptr = 0;
  34.     cout << "*iptr = 0; (iptr = " 
  35.          << *iptr << ")" << endl;
  36.     MemStatus();
  37.  
  38.     // delete the pointer
  39.     delete iptr;
  40.     cout << "delete iptr;" << endl;
  41.     MemStatus();
  42.  
  43.     // delete it again -- ERROR!
  44.     delete iptr;
  45.     MemStatus();
  46.  
  47.     // allocate a block that's too big!
  48.     iptr = new long[32000];
  49.     MemStatus();
  50.  
  51.     return 0;
  52.     }
  53.  
  54. // _new_handler function
  55. void MyNewHandler()
  56.     {
  57.     cout << "\aNEW HANDLER: allocation failure"
  58.          << endl;
  59.     }
  60.  
  61. // display memory allocation status
  62. void MemStatus()
  63.     {
  64.     cout << "\tcount = " 
  65.          << NewDbg::GetCount()
  66.          << ", bytes = " 
  67.          << NewDbg::GetBytes()
  68.          << ", error = " 
  69.          << (int)NewDbg::GetError()
  70.          << endl;
  71.     }
  72.  
  73. // MemDbg error handler
  74. void MemBug(NewDbg::Errors err)
  75.     {
  76.     switch (err)
  77.         {
  78.         case NewDbg::EF_ZEROSIZE:
  79.             cout << "\n\azero-sized allocation"
  80.                  << endl;
  81.             break;
  82.  
  83.         case NewDbg::EF_NOTALLOC:
  84.             cout << "\n\afailed allocation" 
  85.                  << endl;
  86.             break;
  87.  
  88.         case NewDbg::EF_INVALID:
  89.             cout << "\n\adeleted invalid pointer"
  90.                  << endl;
  91.             break;
  92.  
  93.         case NewDbg::EF_UNDERFLOW:
  94.             cout << "\n\atoo much memory deleted"
  95.                  << endl;
  96.             break;
  97.  
  98.         case NewDbg::EF_TOOMANY:
  99.             cout << "\n\atoo many ptrs deleted" 
  100.                  << endl;
  101.             break;
  102.  
  103.         default:
  104.             cout << "This should NEVER happen!"
  105.                  << endl;
  106.         }
  107.  
  108.     NewDbg::ClearError();
  109.     }
  110.